/**
 * 删除字段函数
 * @param {*} obj
 * @param {*} fields
 */
function del_fields(obj, fields = []) {
  for (let field of fields) {
    if (obj[field]) delete obj[field]
  }
  return obj
}

/**
 * 替换字段函数
 * @param {String} field
 * @param {Number} start // 开始位置
 * @param {Number} end // 结束位置
 * @param {String} char
 */
function replace_field(field = '', start = 0, end = -1, char = '*') {
  // 替换后的数据
  let replace = field.slice(start, end).replace(/./g, char)
  return field.slice(0, start) + replace + field.slice(end)
}

const replace_fields = (
  obj,
  fields = [{ field: 'name' }, { field: 'phone', start: 3, end: -4 }]
) => {
  fields.forEach((item) => {
    if (obj[item.field]) {
      // 默认值
      if (item.start == null) item.start = 1
      if (item.end == null) item.end = -1

      // 判断长度
      if (obj[item.field].length <= 2) {
        item.start = 0
        item.end = 1
      }
      obj[item.field] = replace_field(obj[item.field], item.start, item.end)
    }
  })

  return obj
}
module.exports = {
  replace_field,
  del_fields,
  replace_fields,
}
